package com.mtoanmy.lstviewmultichoice;

import java.util.HashMap;
import java.util.List;

import com.mtoanmy.data.ObjItems;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class AdapterListMultiChoice extends BaseAdapter{
	class ViewHolder {
		private String id;
		private View view;

		public ViewHolder(String id, View view) {
			this.id = id;
			this.view = view;
		}

		public String getID() {
			return id;
		}

		public View getView() {
			return view;
		}
	}

	private Activity activity;
	private List<ObjItems> data;
	private HashMap<Integer, ViewHolder> allViews;
	private Context mContext;

	public AdapterListMultiChoice(Context mContext,Activity activity, List<ObjItems> data) {
		this.activity = activity;
		this.data = data;
		this.mContext = mContext;

		allViews = new HashMap<Integer, AdapterListMultiChoice.ViewHolder>();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public ObjItems getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View workView = convertView;

		ObjItems item = data.get(position);

		if (allViews.get(position) == null
				|| !allViews.get(position).getID().equals(item.getID())) {
			workView = new ItemsListMultiChoice(mContext,activity);

			if (item != null) {
				((ItemsListMultiChoice) workView).addData(item);
			}

			allViews.put(position, new ViewHolder(item.getID(), workView));
		} else {
			workView = allViews.get(position).getView();
		}

		return workView;
	}
}
